﻿using BySoft.Server.Business.UserCode;
using BySoft.Server.Common;
using BySoft.Server.Consts;
using BySoft.Web.Controls;
using BySoft.Web.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BySoft.Web.Site.StockManage
{
    public partial class MatInStockList : AuthBasePage
    {

        #region 变量定义
        int m_count = 0;

        public int IPageIndex
        {
            get
            {
                if (null == Session["MatInStockIPageIndex"])
                {
                    return 0;
                }
                else
                {
                    return (int)Session["MatInStockIPageIndex"];
                }
            }
            set
            {
                Session["MatInStockIPageIndex"] = value;
            }
        }

        public int IMatInStockCount
        {
            get
            {
                if (null == Session["IMatInStockCount"])
                {
                    return 0;
                }
                else
                {
                    return (int)Session["IMatInStockCount"];
                }
            }
            set
            {
                Session["IMatInStockCount"] = value;
            }
        }

        private DataTable DtMatInStockListData
        {
            get
            {
                if (null == Session["MatInStockListData"])
                {
                    return CreateQueryDT();
                }
                else
                {
                    return (DataTable)Session["MatInStockListData"];
                }
            }
            set
            {
                Session["MatInStockListData"] = value;
            }
        }

        private DataTable CreateQueryDT()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("BillID");
            dt.Columns.Add("WorkOrderNo");
            dt.Columns.Add("MaterialName");

            dt.Columns.Add("StrFrom");
            dt.Columns.Add("StrTo");
            return dt;
        }

        private void GetQuery()
        {
            DataTable dt = DtMatInStockListData.Copy();
            if (dt != null && dt.Rows.Count > 0)
            {
                DataRow row = dt.Rows[0];
                txtBillID.Text = CommonMethod.GetString(row["BillID"]);
                txtWorkOrderNo.Text = CommonMethod.GetString(row["WorkOrderNo"]);
                txtMaterialName.Text = CommonMethod.GetString(row["MaterialName"]);

                cpFrom.Value = CommonMethod.GetString(row["StrFrom"]);
                cpTo.Value = CommonMethod.GetString(row["StrTo"]);
            }
        }
        #endregion

        #region 初始化及绑定控件
        private void BindData(int pageIndex)
        {
            SetQuery();
            pageIndex = IPageIndex;
            int icurPageIndex = 0;
            string strFrom = CommonMethod.GetStartTime(cpFrom.Value);
            string strTo = CommonMethod.GetEndTime(cpTo.Value);
            DataTable dt = BsMatInStockBusiness.SelectList(txtBillID.Text.Trim(), strFrom, strTo, txtWorkOrderNo.Text.Trim(), txtMaterialName.Text.Trim(), IMatInStockCount, out icurPageIndex, pageIndex, pcData.PageSize, out m_count);

            IMatInStockCount = m_count;
            IPageIndex = pageIndex;
            pcData.RecordCount = m_count;
            if (pageIndex > pcData.PageCount - 1)
            {
                pageIndex = pcData.PageCount - 1;
            }

            if (dt.Rows.Count != 0)
            {
                grdResult.DataSource = dt;
                grdResult.DataBind();
                pcData.CurrentIndex = pageIndex;
                UIHelper.GroupRows(grdResult, "lblBillID", new int[] { 0, 1, 7, 8, 9 });
            }
            else
            {
                UIHelper.BindEmptyGridView(grdResult, dt);
            }
            UIHelper.RunScript(this.Page, "iframeAutoFit();");
        }


        private void SetQuery()
        {
            DataTable dt = CreateQueryDT();
            DataRow row = dt.NewRow();
            row["BillID"] = txtBillID.Text.Trim();
            row["WorkOrderNo"] = txtWorkOrderNo.Text.Trim();
            row["MaterialName"] = txtMaterialName.Text.Trim();

            row["StrFrom"] = cpFrom.Value;
            row["StrTo"] = cpTo.Value;
            dt.Rows.Add(row);
            DtMatInStockListData = dt;
        }

        private void InitData()
        {
            BindData(0);
        }

        private void InitDDL()
        {
        }
        #endregion

        #region 控件事件

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                RemoveSession();
                InitDDL();
                GetQuery();
                InitData();
            }
        }

        private void RemoveSession()
        {
            List<string> lst = GetSession();
            lst.Add("MatInStockIPageIndex");
            lst.Add("MatInStockListData");
            lst.Add("IMatInStockCount");

            ClearSession(lst);
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            BindData(0);
        }

        protected void pcData_PageIndexClick(object sender, PageIndexClickEventArgs e)
        {
            IPageIndex = e.PageIndex;
            BindData(e.PageIndex);
        }

        /// <summary>
        ///   行事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grdResult_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string strCode = e.CommandArgument.ToString();
            string[] strs = strCode.Split(',');
            //string strMention = "";
            switch (e.CommandName)
            {
                case "viewdata":
                    Response.Redirect(string.Format("MatInStockView.aspx?id={0}", HttpUtility.UrlEncode(strCode)));
                    break;
                case "edi":
                    //if (!string.IsNullOrEmpty(BsMatInStock.CheckUsed(strCode)))
                    //{
                    //    UIHelper.AlertMessage(this.Page, "已经生成后置单据,不能编辑");
                    //    return;
                    //}
                    Response.Redirect(string.Format("MatInStockEdit.aspx?id={0}", HttpUtility.UrlEncode(strCode)));
                    break;
                case "del":
                    //if (!string.IsNullOrEmpty(BsMatInStock.CheckUsed(strCode)))
                    //{
                    //    UIHelper.AlertMessage(this.Page, "已经生成后置单据,不能删除");
                    //    return;
                    //}
                    BsMatInStockBusiness.Del(strCode);
                    BindData(0);
                    UIHelper.AlertMessage(this.Page, Message.DeleteSuccess);
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 行数据绑定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grdResult_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                #region 删除绑定
                ImageButton ibtn = (ImageButton)e.Row.FindControl("ibtnDel");
                Button btnDel = (Button)e.Row.FindControl("btnDel");
                if (ibtn != null)
                {
                    ibtn.OnClientClick = "ShowTopConfirm('提示信息','" + Message.ConfirmDelete + "','confirmCallback(\\'" + btnDel.ClientID + "\\')');return false;";
                }
                #endregion
            }
        }
        #endregion
    }
}

